<template>
  <el-dialog
    :model-value="dialogFormVisible"
    :title="title"
    align-center
    draggable
    width="32%"
    style="border-radius: 10px; padding: 5px 10px"
  >
    <el-form :model="form" label-width="120px">
      <el-form-item label="地区">
        <el-input v-model="form.region" style="width: 250px" />
      </el-form-item>
      <el-form-item label="地区系数">
        <el-input v-model="form.coefficient" style="width: 250px" />
      </el-form-item>
      <el-form-item label="是否启用">
        <el-switch v-model="form.state" />
      </el-form-item>
      <el-form-item label="备注说明" class="ins">
        <el-input v-model="form.ins" type="textarea" style="width: 250px" class="ins" />
      </el-form-item>
      <el-form-item>
        <el-button style="width: 100px" type="primary" @click="submitForm"
          >确定</el-button
        >
        <el-button style="width: 100px" @click="onClose">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script setup>
import {
  ref,
  onMounted,
  onBeforeMount,
  reactive,
  defineProps,
  defineEmits,
  watch,
} from "vue";
import { addRegionList } from "@/api/api";
import {
  deleteRegionCoefficient,
  updateRegionCoefficient,
} from "@/api/regionCoefficient";
import { ElMessage } from "element-plus";

const form = reactive({
  id: "",
  region: "",
  coefficient: "",
  state: true,
  ins: "",
});

const dialogFormVisible = ref(false);
const props = defineProps({
  dialogFormVisible: {
    type: Boolean,
    required: true,
  },
  close: {
    type: Function,
    default: () => {},
  },
  update: {
    type: Function,
    default: () => {},
  },
  type: {
    type: String,
    required: true,
  },
  title: {
    type: String,
    required: true,
  },
  selectContent: {
    type: Object,
    required: true,
  },
});

watch(props, (newValue, oldValue) => {
  form.id = newValue.selectContent.id;
  form.region = newValue.selectContent.region;
  form.coefficient = newValue.selectContent.coefficient;
  form.state = newValue.selectContent.state ? true : false;
  form.ins = newValue.selectContent.ins;
});

const onClose = () => {
  props.close();
};

const submitForm = () => {
  
  if (props.type == "add") {
    addRegionList(form).then((data) => {
      if (data.code == 200) {
        ElMessage({
          message: "新增成功",
          type: "success",
        });
        props.close();
        props.update();
      } else {
        ElMessage.error("新增失败");
      }
    });
  } 
  else if (props.type == "edit") {
    updateRegionCoefficient(form).then((data) => {
      if (data.code == 200) {
        ElMessage({
          message: "新增成功",
          type: "success",
        });
        props.close();
        props.update();
      } else {
        ElMessage.error("新增失败");
      }
    });
  }
};
</script>
<style lang="scss" scoped >
::v-deep .el-form-item__label:before {
  content: "* ";
  color: red;
}
.ins {
  ::v-deep .el-form-item__label:before {
    content: " ";
  }
}
</style>